﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using CashFlow.Db.Entity;
using CashFlow.Db.DAL;
using ConsoleApplication1._00_Def;
using ConsoleApplication1._01_Config;
using ConsoleApplication1._02_Common;
using ConsoleApplication1._03_Service;
using ConsoleApplication1._03_Service._00_Db;

namespace ConsoleApplication1._03_Service._01_Jobs
{
    public class ShopPerformanceService
    {
        private static ILog _logger = LogManager.GetLogger(typeof(ShopPerformanceService));

        public static void Calc()
        {
            try
            {
                string date = MyCommonTool.GetOrderDate();
                var orders = OrderService.QueryAll(date);
                if (orders.Count == 0)
                {
                    _logger.Info("未查询到任何订单信息, Date: " + date);
                    return;
                }

                _logger.Info("正在计算店铺业绩: " + date);
                IList<ShopPerformanceEntity> ls = new List<ShopPerformanceEntity>();
                var users = UserService.QueryAll();
                var shops = ShopService.QueryAll();
                var shopUsers = ShopUserService.QueryAll();
                foreach (var item in shops)
                {
                    var ui = users.Where(x => x.Id == item.ShopManagerUserId).FirstOrDefault();
                    if (ui != null && ui.Id > 0)
                    {
                        var childUserIds = UserRelationShipTool.GetShopAllChildUserIds(ui, users, shopUsers);
                        var orders2 = orders.Where(x => childUserIds.Contains(x.BuyerUserId)).ToList();
                        var entity = new ShopPerformanceEntity();
                        entity.ShopManagerUserId = item.ShopManagerUserId;
                        entity.Date = date;
                        decimal sum = orders2.Sum(x => x.Amount);
                        int count = orders2.Count;
                        entity.OrderAmountTotal = sum;
                        entity.OrderTotal = orders2.Count;
                        entity.OrderAmountAvg = sum / count;
                        entity.BonusFee = sum * Consts.BonusPercent;
                        entity.ConsignmentSaleFee = sum * Consts.ConsignSalePercent;

                        entity.AmountFrom0KTo1K = orders2.Where(x => x.Amount >= 0 && x.Amount < 1000).Count();
                        entity.AmountFrom1KTo2K = orders2.Where(x => x.Amount >= 1000 && x.Amount < 2000).Count();
                        entity.AmountFrom2KTo3K = orders2.Where(x => x.Amount >= 2000 && x.Amount < 3000).Count();
                        entity.AmountFrom3KTo4K = orders2.Where(x => x.Amount >= 3000 && x.Amount < 4000).Count();
                        entity.AmountFrom4KTo5K = orders2.Where(x => x.Amount >= 4000 && x.Amount < 5000).Count();
                        entity.AmountFrom5KTo6K = orders2.Where(x => x.Amount >= 5000 && x.Amount < 6000).Count();
                        entity.AmountFrom6KTo7K = orders2.Where(x => x.Amount >= 6000 && x.Amount < 7000).Count();
                        entity.AmountFrom7KTo8K = orders2.Where(x => x.Amount >= 7000 && x.Amount < 8000).Count();
                        entity.AmountFrom8KTo9K = orders2.Where(x => x.Amount >= 8000 && x.Amount < 9000).Count();
                        entity.AmountFrom9KTo10K = orders2.Where(x => x.Amount >= 9000 && x.Amount < 10000).Count();
                        entity.AmountFrom10KTo11K = orders2.Where(x => x.Amount >= 10000 && x.Amount < 11000).Count();
                        entity.AmountFrom11KTo12K = orders2.Where(x => x.Amount >= 11000 && x.Amount < 12000).Count();
                        entity.AmountFrom12KTo13K = orders2.Where(x => x.Amount >= 12000 && x.Amount < 13000).Count();
                        entity.AmountFrom13KTo14K = orders2.Where(x => x.Amount >= 13000 && x.Amount < 14000).Count();
                        entity.AmountFrom14KTo15K = orders2.Where(x => x.Amount >= 14000 && x.Amount < 15000).Count();

                        ls.Add(entity);
                    }
                    else
                    {
                        _logger.Error("错误的ShopManagerUserId: " + item.ShopManagerUserId);
                    }
                }

                using (var db = SqlSugarHelper.GetDb())
                {
                    try
                    {
                        db.BeginTran();

                        db.Deleteable<ShopPerformanceEntity>(x => x.Date == date).ExecuteCommand();
                        db.Insertable<ShopPerformanceEntity>(ls).ExecuteCommand();

                        _logger.Info("准备更新到数据库");
                        db.CommitTran();
                        _logger.Info("更新到数据库成功！");
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                        _logger.Info("更新到数据库失败！");
                        db.RollbackTran();
                    }

                    users.Clear();
                    orders.Clear();
                    shops.Clear();
                    shopUsers.Clear();

                    _logger.Info("计算店铺业绩完成: " + date);
                }
            }
            catch (Exception ex)
            {
                _logger.Fatal(ex);
            }
        }
    }
}
